/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
 /*
 * Actor.fx
 *
 * Created on Apr 7, 2008, 3:55:11 PM
 */

package avgengine.script;

import avgengine.*;
import avgengine.map.*;
import javafx.ui.canvas.*;
import javafx.ui.*;

/**
 * @author ertri
 */

class Actor {
    attribute images:Image*;
    attribute mapImages: Image*;
    attribute music:MusicPlayer*;
    attribute expression:String*;
    attribute style:GenericParagraph;
    
    attribute stage:Stage;
    attribute map : Map;
    attribute sp : Sprite;
    attribute init : boolean;
    attribute exp : int;
    attribute channel: Channel;
    
    attribute msp : MapSprite;
    
    operation initialize();
    operation setStage(s:Stage);
    operation enter(side:int, p:Position);
    operation enterAs(exp:String, side:int, p:Position);
    operation fadeInAs(exp:String,p:Position);
    operation fadeIn(p:Position);
    operation fadeOut();
    operation move(p:Position);
    operation quickMove(p:Position);
    operation exit(side:int);
    operation change(exp:String);
    operation quickChange(exp:String);
    
    operation show(x:int, y:int);
    operation moveOnMap(x:int, y:int);
    operation hide();
    
    
}


attribute Actor.exp = -1;

attribute Actor.channel = Channel{};


class Position {
    attribute x:int;
    attribute name:String;
}

class Channel {
    attribute music:MusicPlayer;
    operation setMusic(m:MusicPlayer);
    operation stop();
    operation start();
    operation clear();
}

operation Channel.setMusic(m) {
    if(m<>null) {
        music.stop();
        this.music = m;
        music.start();
    }
}

operation Channel.stop() {
    music.stop();
}

operation Channel.start() {
    music.start();
}

operation Channel.clear() {
    music.stop();
    music = null;
}

operation Actor.setStage(s) {
    stage = s;
    if (not init) {
        initialize();
    }
    //    sp.exit(LEFT:int);
}

operation Actor.initialize() {
    for(m in music) {
        m.load();
    }
    sp = Sprite{imageSet:images, stage:bind stage halignment:CENTER,valignment:BOTTOM};
    msp = MapSprite{imageSet: mapImages};
    
}

operation Actor.enter(side, p) {
    channel.start();
    sp.enter(side,p.x);
}

operation Actor.enterAs(exp, side, p) {
    println("set transparent");
    sp.opacity = 0;
    println("change to exp");
    quickChange(exp);
    println("quick exit");
    sp.quickExit(side);
    println("set opaque");
    sp.opacity = 1;
    println("enter stage");
    sp.enter(side,p.x);
}
operation Actor.fadeInAs(exp, p) {
    sp.opacity = 0;
    println("change to exp");
    quickChange(exp);
    sp.quickAdjust(p.x);
    quickMove(p);
    println("{sp.x},{sp.y}");
    sp.quickClear();
    sp.opacity = 1;
    println("enter stage");
    change(exp);
    
}
operation Actor.fadeOut() {
    sp.clearText();
}
operation Actor.fadeIn(p) {
    fadeInAs(expression[exp],p);
}

operation Actor.exit(side) {
    sp.exit(side);
    channel.stop();
}

operation Actor.move(p) {
    sp.move(p.x);
}

operation Actor.quickMove(p) {
    sp.quickMove(p.x);
}
operation Actor.change(exp) {
    var su = Utilities{};
    this.exp = su.find(expression, exp);
    channel.setMusic(    music[this.exp]);
    sp.show(this.exp);
    if(mapImages[this.exp]<>null) {
        msp.show(this.exp);
    }
    
}

operation Actor.quickChange(exp) {
    var su = Utilities{};
    this.exp = su.find(expression, exp);
    channel.setMusic(music[this.exp]);
    println("show picture");
    sp.quickShow(this.exp);
    if(mapImages[this.exp]<>null) {
        msp.quickShow(this.exp);
    }
    
}

operation Actor.moveOnMap(x, y) {
    msp.moveOff(x, y);
}
operation Actor.hide() {
    msp.opacity = 0;
}
operation Actor.show(x, y) {
    
    this.msp.quickMove([x, y]);
    this.msp.quickShow(this.exp);
    // doable without stage since layout is TOP, LEFT
    this.msp.gx = x;
    this.msp.gy = y;
    map.addSprite(msp);
}



